(function(d){
	
	//link to popup
	var className = 'external', int_width = 720, int_height = 630;
	var as = document.getElementsByTagName('a');
	var int_windowLeft = (screen.width - int_width) / 2;
	var int_windowTop = (screen.height - int_height) / 2;
	for(i=0;i<as.length;i++){
		var a = as[i];
		var r=new RegExp("(^| )"+className+"($| )");
		if(r.test(a.className)){
			a.onclick = function(){
				window.open(this.href,"DescriptiveWindowName","width="+int_width+",height="+int_height+",resizable=yes,scrollbars=yes,status=yes,left="+int_windowLeft+",top="+int_windowTop);
				return false;
			}
		}
	}
	
	function ps(password, username) {
		var score = 0;
		if (password.length < 6) return { t: 'Contraseña demasiado corta', s: score};
		if (password.toLowerCase() == username.toLowerCase()) return { t: 'La contraseña es igual al nombre de usuario', s: score};
		score += password.length * 4;
		score += (ps_checkRepetition(1, password).length - password.length) * 1;
		score += (ps_checkRepetition(2, password).length - password.length) * 1;
		score += (ps_checkRepetition(3, password).length - password.length) * 1;
		score += (ps_checkRepetition(4, password).length - password.length) * 1;
		if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) score += 5;
		if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5;
		if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) score += 10;
		if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) score += 15;
		if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)) score += 15;
		if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)) score += 15;
		if (password.match(/^\w+$/) || password.match(/^\d+$/)) score -= 10;
		if (score < 0) score = 0;
		if (score > 100) score = 100;
		if (score < 34) return { t: 'Combina letras y números', s: score};
		if (score < 68) return { t: 'Seguridad media', s: score };
		return { t: 'Contraseña segura', s: score };
	}
	function ps_checkRepetition(pLen, str) {
		var res = "";
		for (i = 0; i < str.length; i++) {
			var repeated = true;
			for (j = 0; j < pLen && (j + i + pLen) < str.length; j++) repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + pLen));
			if (j < pLen) repeated = false;
			if (repeated) {
				i += pLen - 1;
				repeated = false;
			} else  res += str.charAt(i);
		}
		return res;
	}

	// focusblur
	var fb = function (t) {
		t.onblur = function(){
			if (t.value==""){
				t.value = t.title; 
				if (this.type=='password'){
					this.style.display = 'none';
					this.form[this.name+'_caption'].style.display = 'inline';
				}
			}
		}
		t.onfocus = function(){
			if (this.value==this.title || (t.parentNode.className.indexOf('error')>-1) ) {
				this.value="";
				if (this.name.indexOf('_caption')>-1) {
				//if (this.name=='clave_caption') {
					this.style.display = 'none';
					this.form[ this.name.replace('_caption','') ].style.display = 'inline';
					this.form[ this.name.replace('_caption','') ].focus();
				}
				if (t.parentNode.className.indexOf('error')>-1) { 
					t.parentNode.className = t.parentNode.className.replace(/error/gi,'');
				}
			}
		}
	}
	var fbpass = function (t) {
		var _e = d.getElementById('clave_caption');
		_e.onblur = function(){
			t.style.display = 'none';
			_e.style.display = 'block';
			_e.focus();
		}
		_e.onfocus = function(){
			_e.style.display = 'none';
			t.style.display = 'block';
			_e.focus();
		}
	}	

	var pr = function(_o,_s){
		_o = _o.previousSibling;
		if (_o==null) return false;
		if (_o.nodeName.toLowerCase() == _s) return _o;
		else return (pr(_o,_s));
	}

	var is = d.getElementsByTagName('input');
	for (var i=0; i<is.length;i++){
		if (is[i].type=='text' || is[i].type=='password') fb(is[i]);
		else if (is[i].type=='checkbox' || is[i].type=='radio'){
			is[i].onfocus = function(){
				this.parentNode.className = this.parentNode.className.replace(/error/gi,'');
			}
		}
	}

	var is = d.getElementsByTagName('select');
	for (var i=0; i<is.length;i++){
		if (is[i].name == 'pais'){
			is[i].onchange = function(){
				if (this.value != 'PE') d.getElementById('row-ciudad').style.display = 'none';
				else d.getElementById('row-ciudad').style.display = 'inline';
			}
		}
		is[i].onfocus = function(){
			this.parentNode.className = this.parentNode.className.replace(/error/gi,'');
		}		
	}
	

	var fs = d.getElementsByTagName('form');
	for (var i=0; i<fs.length;i++){
		fs[i].onsubmit = function(_p){
			var _is = this.getElementsByTagName('input'), error = false;
			for (var i=0; i<_is.length;i++){
				var _i = _is[i];
				if (_i.type=='text' || _i.type=='password' ) {
					
					var _er = false;
					<!--if (_i.className.indexOf('i-mail')>-1 && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(_i.value))  ) _er = true;
					<!--else if(_i.className.indexOf('i-char')>-1){
						//if ( !(/([^\s])/.test(_i.value)) || _i.title == _i.value) _er = true;
				<!--		if ( !(/([^\s])/.test(_i.value)) || _i.title == _i.value || _i.value == (/*'Ingresar '+*/_i.title)) _er = true;
				<!--	}
					if (_er){
						_i.parentNode.className = _i.parentNode.className.replace(' error','') + ' error';
						if (_i.title!==""){
							if (_i.type == 'password') this[_i.name+'_caption'].value = /*'Ingresar '  + */ _i.title;
							else _i.value = /*'Ingresar ' + */_i.title;
						}
						error = true;
					}
				}else if (_i.type=='checkbox') {
					if (_i.className.indexOf('i-char')>-1 && !_i.checked) {
						_i.parentNode.className = _i.parentNode.className.replace(/error/gi,'') + ' error';
						error = true;
					}
				}else if (_i.type=='radio') {
					if (_i.className.indexOf('i-char')>-1) {
						if (_i.name=='genero'){
							if (!(this.genero[0].checked || this.genero[1].checked)){
								_i.parentNode.className = _i.parentNode.className.replace(/error/gi,'') + ' error';
								error = true;
							}
						}
					}
				}
			}
			var _is = this.getElementsByTagName('select');
			for (var i=0; i<_is.length;i++){
				var _i = _is[i];
					if (_i.className.indexOf('i-char')>-1 &&   !(/([^\s])/.test(_i.value))  ) {
						_i.parentNode.className = _i.parentNode.className.replace(/error/gi,'') + ' error';
						error=true;
					}
			}

			if (this.submitted) this.submitted.value = 1;
			if (error || _p == "force") return false;
		}
	}
	

	var pp = d.getElementById('pi_pass');
	if (pp != null) pp.onkeyup = function(){
		var _r = ps(this.value,d.getElementById('pi_user').value) ;
		d.getElementById('percent').innerHTML = _r.s+' %';
		d.getElementById('result').innerHTML = _r.t;
		var _cl = d.getElementById('colorbar');
		_cl.style.backgroundPosition = '0px -'+_r.s+'px';
		_cl.style.width = (_r.s * 2.88)+'px';
	}
	

	var lm = d.getElementById('mas-op');
	if (lm !=null){
		var ld = d.getElementById('mas-det');
	
		lm.parentNode.onmouseover = function(e){ sh(true); }
		lm.parentNode.onmouseout = function(e){ sh(false); }
		var sh = function(show){
			lm.style.display = show?'none':'block';
			ld.style.display = show?'block':'none';
		}
	}
	//tmp: force exec. history.back();
	var _f = d.getElementsByTagName('form')[0];
	if (_f && _f.submitted && (_f.submitted.value != "0" )){
		var _p = d.getElementById('pais'); if (_p) _p.onchange();
		_f.onsubmit('force');
		_f.submitted.value = _f.submitted.defaultValue;
	}
}(document));